﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Collections;
using System.Text;
using System.IO;
using System.Xml;
using System.Windows.Forms;
using System.Xml.Serialization;
using OpenESSOConfigurator.PresentationLayer.Components;
using OpenESSOConfigurator.BusinessLayer.Serializer;
using OpenESSOConfigurator.BusinessLayer;
using OpenESSOConfigurator.BusinessLayer.Atoms;
using OpenESSOConfigurator.PresentationLayer.PresentationModels;

namespace OpenESSOConfigurator.InterfaceLayer.Forms
{
    public partial class MainForm : System.Windows.Forms.Form
    {
        public MainForm()
        {
            InitializeComponent();
            // TODO: make initialisation by last openedfile
            openFileDialog1.FileName = System.Windows.Forms.Application.StartupPath + "\\..\\..\\OpenESSOConfigurator.xml";
        }

        private void LoadOpenESSOConfigFile(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
        }

        
        private void mainFormBgdWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            this.mainFormBackgroundWorker.ReportProgress(10, "Importing object");
            StreamReader stream = new StreamReader(e.Argument.ToString());
            this.mainFormBackgroundWorker.ReportProgress(20, "Importing object");
            StringReader stringReader = new StringReader(stream.ReadToEnd());
            stream.Close();
            AtomsSerializer<OpenESSOConfiguration> configurationSerializer = new AtomsSerializer<OpenESSOConfiguration>();
            this.mainFormBackgroundWorker.ReportProgress(30, "Importing object");
            configurationSerializer.AtomDeserializer(stringReader);
            this.mainFormBackgroundWorker.ReportProgress(60, "Importing object");
            ItemTreePresentationModel.Instance.OpenESSOConfiguration = (OpenESSOConfiguration)configurationSerializer.Generic;
            this.mainFormBackgroundWorker.ReportProgress(90, "Importing object");
            
        }

        private void mainFormBgdWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            toolStripStatusLabel1.Text = e.UserState.ToString();
            toolStripProgressBar1.Value = e.ProgressPercentage;

        }

        private void mainFormBgdWorker_RunWorkerComplet(object sender, RunWorkerCompletedEventArgs e)
        {
            toolStripStatusLabel1.Text = "Finished";
            toolStripProgressBar1.Value = 0;

        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            this.mainFormBackgroundWorker.RunWorkerAsync(this.openFileDialog1.FileName);
        }
    }
}
